<?php

namespace app\helpers;
use app\libs\Parser;

class Game{
	
	/**
	 * 检测用户答题结果
	 */
	public static function check($str) {
		Logger::add("Post str:".$str);
		
		$room = \app\manager\Room::instance()->getCurrent();
		if(!$room) return false;
		
		list($numbers, $solution) = $room->getQuestion();
		
		$str = trim($str);
		
		if(!$solution) return (int)$str === -1;
		
		foreach($numbers as $item) {
			if( false === strpos($str, (string)$item) ) {
				Logger::add("No number: ".$item);
				return false;
			}
		}
		
		$tmp = str_replace(['(', ')'], '', $str);
		if(!preg_match("#^(?:\d[\+\-\*\/]){3}\d$#", $tmp)) {
			Logger::add("Error expression.");
			return false;
		}
		
		try{
			$val = Parser::parseStr($str);
		} catch(\Exception $e) {
			$val = 0;
		}
		
		return $val == 24;
	}
	
}